//
//  UserInfoTableViewController.m
//  MooreDoll
//
//  Created by 郭景豪 on 2014/12/22.
//  Copyright (c) 2014年 Tank. All rights reserved.
//

#import "UserInfoTableViewController.h"
#import "UserInfoFor0Cell.h"
#import "UserInfoFor1Cell.h"
#import "UserInfoFor2Cell.h"
#import "UserInfoFor3Cell.h"
#import "RoundImage.h"
#import "UITableView+Separator.h"
#import "ChangeAccountViewController.h"
#import "ChangePasswordViewController.h"
#import "ChangeNickNameViewController.h"

#import "WriteToFileImage.h"

@interface UserInfoTableViewController ()<UserInfoFor0CellDelegate>

@end

@implementation UserInfoTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.userInfo = [UserInfoModel sharedInstance];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeUserInfo:) name:@"changeUserInfo" object:nil];
    UIView *footer =[[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.tableFooterView = footer;
}
-(void)changeUserInfo:(NSNotification *)notification
{
    self.userInfo = [notification object];
    [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //設定第一個cell 高度 （使用者圖片資料）
    if (indexPath.row==0) {
        //依照螢幕寬度來設定Cell 高度
        return CGRectGetWidth([UIScreen mainScreen].bounds)*0.400;
    }
    
    //依照螢幕寬度來設定Cell 高度
    return CGRectGetWidth([UIScreen mainScreen].bounds)*0.1375;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 4;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.row) {
        case 0:
            return [self onePage:tableView cellForIndexPath:indexPath];
            break;
        case 1:
            return [self twoPage:tableView cellForIndexPath:indexPath];

            break;
        case 2:
            return [self ThreePage:tableView cellForIndexPath:indexPath];

            break;
        case 3:
            return [self FourPage:tableView cellForIndexPath:indexPath];
            break;
        default:
            return nil;
            break;
    }
    // Configure the cell...
    
}
-(UserInfoFor0Cell *)onePage:(UITableView *)tableView cellForIndexPath:(NSIndexPath *)indexPath
{
    UserInfoFor0Cell *cell = (UserInfoFor0Cell*)[tableView dequeueReusableCellWithIdentifier:@"UserInfoFor0Cell"];
    
    if (cell == nil) {
        
        [tableView registerNib:[UINib nibWithNibName:@"UserInfoFor0Cell" bundle:nil] forCellReuseIdentifier:@"UserInfoFor0Cell"];
        
        cell = [tableView dequeueReusableCellWithIdentifier:@"UserInfoFor0Cell"];
    }
    if (self.userInfo.ImagePath)
    {
        [cell.photoButton setImage:[RoundImage maskImage:[UIImage imageWithContentsOfFile:self.userInfo.ImagePath] withMask:[UIImage imageNamed:@"mask"]] forState:UIControlStateNormal];

    }else
    {
        [cell.photoButton setImage:[RoundImage maskImage:[UIImage imageNamed:@"userImage"] withMask:[UIImage imageNamed:@"mask"]] forState:UIControlStateNormal];

    }
    if (cell.delegate != self) {
        cell.delegate = self;

    }
    
    return cell;
}
-(UserInfoFor1Cell *)twoPage:(UITableView *)tableView cellForIndexPath:(NSIndexPath *)indexPath
{
    UserInfoFor1Cell *cell = (UserInfoFor1Cell*)[tableView dequeueReusableCellWithIdentifier:@"UserInfoFor1Cell"];
    
    if (cell == nil) {
        
        [tableView registerNib:[UINib nibWithNibName:@"UserInfoFor1Cell" bundle:nil] forCellReuseIdentifier:@"UserInfoFor1Cell"];
        
        cell = [tableView dequeueReusableCellWithIdentifier:@"UserInfoFor1Cell"];
    }
    cell.textLabel.text = @"手機";
    cell.detailTextLabel.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"account"]?[[NSUserDefaults standardUserDefaults]objectForKey:@"account"]:@"無名氏";

    return cell;
}
-(UserInfoFor2Cell *)ThreePage:(UITableView *)tableView cellForIndexPath:(NSIndexPath *)indexPath
{
    UserInfoFor2Cell *cell = (UserInfoFor2Cell*)[tableView dequeueReusableCellWithIdentifier:@"UserInfoFor2Cell"];
    
    if (cell == nil) {
        
        [tableView registerNib:[UINib nibWithNibName:@"UserInfoFor2Cell" bundle:nil] forCellReuseIdentifier:@"UserInfoFor2Cell"];
        
        cell = [tableView dequeueReusableCellWithIdentifier:@"UserInfoFor2Cell"];
    }
    cell.textLabel.text = @"姓名或暱稱";
    if (self.userInfo.NICKNAME)
    {
        cell.detailTextLabel.text = [self.userInfo.NICKNAME stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    }else
    {
        cell.detailTextLabel.text = @"沒有名字";
    }

    return cell;
}
-(UserInfoFor3Cell *)FourPage:(UITableView *)tableView cellForIndexPath:(NSIndexPath *)indexPath
{
    UserInfoFor3Cell *cell = (UserInfoFor3Cell*)[tableView dequeueReusableCellWithIdentifier:@"UserInfoFor3Cell"];
    
    if (cell == nil) {
        
        [tableView registerNib:[UINib nibWithNibName:@"UserInfoFor3Cell" bundle:nil] forCellReuseIdentifier:@"UserInfoFor3Cell"];
        
        cell = [tableView dequeueReusableCellWithIdentifier:@"UserInfoFor3Cell"];
    }
    cell.textLabel.text = @"更改密碼";
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *alertViewString = [NSString stringWithFormat:@"您目前已綁定%@的手機號碼作為您的帳號，如要更換號碼需要重新進行認證的程序，您確定要更改手機號碼？",[[NSUserDefaults standardUserDefaults]objectForKey:@"account"]];
    ChangePasswordViewController *changePasswordVC;
    ChangeNickNameViewController *changeNickNameVC;
    switch (indexPath.row) {
        case 0:
            
            break;
        case 1:
            [[[UIAlertView alloc]initWithTitle:@"" message:alertViewString delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確認",nil]show];
            break;
        case 2:
            changeNickNameVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ChangeNickNameViewController"];
            [self.navigationController pushViewController:changeNickNameVC animated:YES];
            break;
        case 3:
            changePasswordVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ChangePasswordViewController"];
            [self.navigationController pushViewController:changePasswordVC animated:YES];
            break;
        default:
            break;
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1) {
        ChangeAccountViewController* changeAccountVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ChangeAccountViewController"];
        [self.navigationController pushViewController:changeAccountVC animated:YES];

    }
}


#pragma mark 大頭貼 delegate
-(void)cellTakePhotoButtonOpenCamera:(UIImagePickerController *)camera
{
    [self presentViewController:camera animated:YES completion:nil];
}
-(void)cellTakePhotoButtonOpenPhotoLibrary:(UIImagePickerController *)PhotoLibrary
{
    [self presentViewController:PhotoLibrary animated:YES completion:nil];
}
-(void)cellTakePhotoAlertDismissVC
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            UserInfoFor0Cell *cell = [[self.tableView visibleCells]firstObject];

            NSString *userImagePath;
            
            if (cell.photoButton.imageView.image != nil) {
                //儲存使用者的圖片
                userImagePath = [[WriteToFileImage sharedInstance]GetAndWriteToFileImage:cell.photoButton.imageView.image];
            }else
            {
                //儲存使用者的圖片
                userImagePath = [[WriteToFileImage sharedInstance]GetAndWriteToFileImage:[UIImage imageNamed:@"dficon_user"]];
            }
            
            if (userImagePath != nil) {
                //將名字還有 圖片路徑包進字典
                NSDictionary *dict = @{@"userImage":userImagePath,@"userName":[UserInfoModel sharedInstance].NICKNAME ? [UserInfoModel sharedInstance].NICKNAME :@"無名氏"};
                
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    
                    while (dict == nil) {
                    }
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [[XMPPSharedInstance sharedInstance]changeVCard:dict];
                        
                    });
                    
                });                //執行變更名字和照片
            }
        });
    }];
    
    
}
-(void)cellTakePhotoClance
{
    [self dismissViewControllerAnimated:YES completion:nil];

}
@end
